home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / f2c / may_5_92.lha / f2c.VMay_5_1992 / libI77 / open.c < prev    next >
C/C++ Source or Header  |  1992-05-07  |  4KB  |  192 lines

  1. #include "sys/types.h"
  2. #ifndef MSDOS
  3. #include "sys/stat.h"
  4. #endif
  5. #include "f2c.h"
  6. #include "fio.h"
  7. #include "string.h"
  8. #include "fcntl.h"
  9. #ifndef O_WRONLY
  10. #define O_RDONLY 0
  11. #define O_WRONLY 1
  12. #endif
  13. #define NON_ANSI_RW_MODES
  14.  
  15. extern char *malloc(), *mktemp();
  16. extern FILE *fdopen();
  17. extern integer f_clos();
  18. #ifdef NON_ANSI_RW_MODES
  19. char *r_mode[2] = {"r", "r"};
  20. char *w_mode[2] = {"w", "w"};
  21. #else
  22. char *r_mode[2] = {"rb", "r"};
  23. char *w_mode[2] = {"wb", "w"};
  24. #endif
  25.  
  26. integer f_open(a) olist *a;
  27. {    unit *b;
  28.     int n;
  29.     char buf[256];
  30.     cllist x;
  31. #ifndef MSDOS
  32.     struct stat stb;
  33. #endif
  34.     if(a->ounit>=MXUNIT || a->ounit<0)
  35.         err(a->oerr,101,"open")
  36.     curunit = b = &units[a->ounit];
  37.     if(b->ufd) {
  38.         if(a->ofnm==0)
  39.         {
  40.         same:    if (a->oblnk)
  41.                 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
  42.             return(0);
  43.         }
  44. #ifdef MSDOS
  45.         if (b->ufnm
  46.          && strlen(b->ufnm) == a->ofnmlen
  47.          && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
  48.             goto same;
  49. #else
  50.         g_char(a->ofnm,a->ofnmlen,buf);
  51.         if (inode(buf,&n) == b->uinode && n == b->udev)
  52.             goto same;
  53. #endif
  54.         x.cunit=a->ounit;
  55.         x.csta=0;
  56.         x.cerr=a->oerr;
  57.         if((n=f_clos(&x))!=0) return(n);
  58.         }
  59.     b->url=a->orl;
  60.     b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
  61.     if(a->ofm==0)
  62.     {    if(b->url>0) b->ufmt=0;
  63.         else b->ufmt=1;
  64.     }
  65.     else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
  66.     else b->ufmt=0;
  67. #ifdef url_Adjust
  68.     if (b->url && !b->ufmt)
  69.         url_Adjust(b->url);
  70. #endif
  71.     if (a->ofnm) {
  72.         g_char(a->ofnm,a->ofnmlen,buf);
  73.         if (!buf[0])
  74.             err(a->oerr,107,"open")
  75.         }
  76.     else
  77.         sprintf(buf, "fort.%ld", a->ounit);
  78.     b->uscrtch = 0;
  79.     switch(a->osta ? *a->osta : 'u')
  80.     {
  81.     case 'o':
  82.     case 'O':
  83. #ifdef MSDOS
  84.         if(access(buf,0))
  85. #else
  86.         if(stat(buf,&stb))
  87. #endif
  88.             err(a->oerr,errno,"open")
  89.         break;
  90.      case 's':
  91.      case 'S':
  92.         b->uscrtch=1;
  93.         (void) strcpy(buf,"tmp.FXXXXXX");
  94.         (void) mktemp(buf);
  95.         (void) close(creat(buf, 0666));
  96.         break;
  97.     case 'n':
  98.     case 'N':
  99. #ifdef MSDOS
  100.         if(!access(buf,0))
  101. #else
  102.         if(!stat(buf,&stb))
  103. #endif
  104.             err(a->oerr,128,"open")
  105.         /* no break */
  106.     case 'r':    /* Fortran 90 replace option */
  107.     case 'R':
  108.         (void) close(creat(buf, 0666));
  109.         break;
  110.     }
  111.  
  112.     b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
  113.     if(b->ufnm==NULL) err(a->oerr,113,"no space");
  114.     (void) strcpy(b->ufnm,buf);
  115.     b->uend=0;
  116.     b->uwrt = 0;
  117.     if(isdev(buf))
  118.     {    b->ufd = fopen(buf,r_mode[b->ufmt]);
  119.         if(b->ufd==NULL) err(a->oerr,errno,buf)
  120.     }
  121.     else {
  122.         if((b->ufd = fopen(buf, r_mode[b->ufmt])) == NULL) {
  123.             if ((n = open(buf,O_WRONLY)) >= 0) {
  124.                 b->uwrt = 2;
  125.                 }
  126.             else {
  127.                 n = creat(buf, 0666);
  128.                 b->uwrt = 1;
  129.                 }
  130.             if (n < 0
  131.             || (b->ufd = fdopen(n, w_mode[b->ufmt])) == NULL)
  132.                 err(a->oerr, errno, "open");
  133.             }
  134.     }
  135.     b->useek=canseek(b->ufd);
  136. #ifndef MSDOS
  137.     if((b->uinode=inode(buf,&b->udev))==-1)
  138.         err(a->oerr,108,"open")
  139. #endif
  140.     if(a->orl && b->useek) rewind(b->ufd);
  141.     return(0);
  142. }
  143. fk_open(seq,fmt,n) ftnint n;
  144. {    char nbuf[10];
  145.     olist a;
  146.     (void) sprintf(nbuf,"fort.%ld",n);
  147.     a.oerr=1;
  148.     a.ounit=n;
  149.     a.ofnm=nbuf;
  150.     a.ofnmlen=strlen(nbuf);
  151.     a.osta=NULL;
  152.     a.oacc= seq==SEQ?"s":"d";
  153.     a.ofm = fmt==FMT?"f":"u";
  154.     a.orl = seq==DIR?1:0;
  155.     a.oblnk=NULL;
  156.     return(f_open(&a));
  157. }
  158. isdev(s) char *s;
  159. {
  160. #ifdef MSDOS
  161.     int i, j;
  162.  
  163.     i = open(s,O_RDONLY);
  164.     if (i == -1)
  165.         return 0;
  166.     j = isatty(i);
  167.     close(i);
  168.     return j;
  169. #else
  170.     struct stat x;
  171.  
  172.     if(stat(s, &x) == -1) return(0);
  173. #ifdef S_IFMT
  174.     switch(x.st_mode&S_IFMT) {
  175.         case S_IFREG:
  176.         case S_IFDIR:
  177.             return(0);
  178.         }
  179. #else
  180. #ifdef S_ISREG
  181.     /* POSIX version */
  182.     if(S_ISREG(x.st_mode) || S_ISDIR(x.st_mode))
  183.         return(0);
  184.     else
  185. #else
  186.     Help! How does stat work on this system?
  187. #endif
  188. #endif
  189.         return(1);
  190. #endif
  191. }
  192.